package com.teatime.client.view;

import com.teatime.client.R;
import com.teatime.client.TLog;
import com.teatime.client.TeaTimeUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TFrame extends RelativeLayout {

	protected int       mTitleBarId;
	protected ViewGroup mTitleBar;
	protected int       mTopBarId;
	protected ViewGroup mTopBar;
	protected int       mBottomBarId;
	protected ViewGroup mBottomBar;
	protected int       mLayoutId;
	protected ViewGroup mMainLayout;
	protected ViewGroup mContentArea;
	
	protected String    mTitleLeftBtnText;
	protected String    mTitleRightBtnText;
	protected String 	mTitle;
	protected TextView  mTxtTitle;
	protected Button    mBtnTitleLeft;
	
	protected LayoutInflater mLayoutFactory;
	
	private TFrameListener 	 mTFrameListener;
	private OnTFrameListener mListener;
	
	public TFrame(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);
	}
	
	protected LayoutInflater getLayoutFactory() {
		if (mLayoutFactory == null) {
			mLayoutFactory = LayoutInflater.from(getContext());
		}
		return mLayoutFactory;
	}
	
	protected View createViewById(int viewId) {
		if (viewId <= 0) {
			return null;
		}
		LayoutInflater factory = getLayoutFactory();
		return factory.inflate(viewId, null);
	}
	
	private static class TFrameListener implements 
			OnClickListener {
		private TFrame mFrame;
		public TFrameListener(TFrame frame) {
			mFrame = frame;
		}
		
		@Override
		public void onClick(View view) {
			mFrame.onButtonClicked(view);
		}

	};
	
	protected void onButtonClicked(View btn) {
		if (btn == mBtnTitleLeft) {
			if (mListener != null) {
				mListener.onTitleClicked(this, true);
			}
		}
	}
	
	protected void onTitleLeftButtonClicked() {
		if (mListener != null) {
			mListener.onTitleClicked(this, true);
		}
	}
	
	public interface OnTFrameListener {
		public void onTitleClicked(TFrame frame, boolean left);
		public void onBackKeyDown(TFrame frame);
	}
	
	public void setOnBackListener(OnTFrameListener l) {
		mListener = l;
	}
	
	private TFrameListener getTFrameListener() {
		if (mTFrameListener == null) {
			mTFrameListener = new TFrameListener(this);
		}
		return mTFrameListener;
	}
	
	protected void init(AttributeSet attrs) {
		mTitleLeftBtnText  = null;
		mTitleRightBtnText = null;
		mTitleBarId     = R.layout.view_win_title_def;
		mTitleBar       = null;
		mTopBarId       = -1;
		mBottomBarId 	= -1;
		mTopBar    		= null;
		mBottomBar 		= null;
		mLayoutId       = R.layout.view_tframe;
		mContentArea 	= null;
		initAttrs(attrs);
		initViews();
	}
	
	protected void initAttrs(AttributeSet attrs) {
		TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.teatime);
		if (a == null) {
			return;
		}
		
		for (int i = 0; i < a.getIndexCount(); i ++) {
            int attr = a.getIndex(i);
            switch (attr) {
            case R.styleable.teatime_titleText:
            	mTitle = a.getString(attr);
                break;
            case R.styleable.teatime_titleLeftButton:
            	mTitleLeftBtnText = a.getString(attr);
                break;
            case R.styleable.teatime_titleRightButton:
            	mTitleRightBtnText = a.getString(attr);
                break;
            case R.styleable.teatime_titleLayout:
            	mTitleBarId = a.getResourceId(attr, mTitleBarId);
            	break;
            case R.styleable.teatime_topBar:
            	mTopBarId = a.getResourceId(attr, -1);
            	break;
            case R.styleable.teatime_bottomBar:
            	mBottomBarId = a.getResourceId(attr, -1);
            	break;
            case R.styleable.teatime_layout:
            	mLayoutId = a.getResourceId(attr, mLayoutId);
            	break;
            }
        }
        a.recycle();
	}
	
	public Window getWindow() {
		return ((Activity)getContext()).getWindow();
	}
	
	protected void initViews() {
		mMainLayout = (ViewGroup)createViewById(mLayoutId);
		if (mMainLayout != null) {
			initTitleBar();
			initTopBar();
			initBottomBar();
			initContentArea();
			addView(mMainLayout);
		}
		
	}
/*
	public ViewGroup createAreaLayout(boolean vertical, float weight) {
		LinearLayout layout = new LinearLayout(getContext());
		if (vertical) {
			layout.setOrientation(LinearLayout.VERTICAL);
		}
		else {
			layout.setOrientation(LinearLayout.HORIZONTAL);
		}
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)layout.getLayoutParams();
		if (lp == null) {
			lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
		}
		lp.width  = ViewGroup.LayoutParams.FILL_PARENT;
		lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
		lp.weight = weight;
		layout.setLayoutParams(lp);
		return layout;
	}
	*/
	public ViewGroup createArea(int areaId, int barId) {
		View view = createViewById(barId);
		if (view == null) {
			return null;
		}
		
		ViewGroup area = (ViewGroup)mMainLayout.findViewById(areaId);
		if (area != null) {
			area.addView(view, 
					ViewGroup.LayoutParams.FILL_PARENT, 
					ViewGroup.LayoutParams.WRAP_CONTENT);
		}
		return area;
	}
	
	public void initTopBar() {
		if (mTopBarId > 0) {
			mTopBar = createArea(R.id.tframe_top_area, mTopBarId);
			TLog.v("initTopBar: " + mTopBar);
		}
	}
	
	public void initBottomBar() {
		if (mBottomBarId > 0) {
			mBottomBar = createArea(R.id.tframe_bottom_area, mBottomBarId);
			TLog.v("initBottomBar: " + mBottomBar);
		}
	}
	
	public void initContentArea() {
		mContentArea = (ViewGroup)mMainLayout.findViewById(R.id.tframe_content_area);
		TLog.v("initContentArea: " + mContentArea);
	}
	
	public void initTitleBar() {
		View titleView = createViewById(mTitleBarId);
		if (titleView == null) {
			return;
		}

		mTxtTitle = (TextView)titleView.findViewById(R.id.txt_win_title);
		if (mTxtTitle != null) {
			mTxtTitle.setText(mTitle);
		}
		
		mBtnTitleLeft  = (Button)titleView.findViewById(R.id.btn_win_title_back);
		if (mBtnTitleLeft != null) {
			if (mTitleLeftBtnText == null 
				|| mTitleLeftBtnText == "") {
				mBtnTitleLeft.setVisibility(GONE);
			}
			else {
				mBtnTitleLeft.setVisibility(VISIBLE);
				mBtnTitleLeft.setText(mTitleLeftBtnText);
			}
			
			mBtnTitleLeft.setOnClickListener(getTFrameListener());
		}

		mTitleBar = (ViewGroup)mMainLayout.findViewById(R.id.tframe_title_area);
		mTitleBar.addView(titleView);
		
		TLog.v("initTitleBar: " + mTitleBar);
	}
	
	public void onCreate() {
		//this.setFocusable(true);
		//this.setFocusableInTouchMode(true);
	}
	
	@Override
	public void addView(View child, int index, ViewGroup.LayoutParams params) {
		TLog.v("TFrame.addView: " + child + ", " + mMainLayout);
		if (mContentArea != null
			&& child != mMainLayout) {
			mContentArea.addView(child, index, params);
		}
		else {
			super.addView(child, index, params);
		}
	}
	
	public int getScreenWidth() {
		return TeaTimeUtils.getScreenWidth(getContext());
	}
	
	public int getScreenHeight() {
		return TeaTimeUtils.getScreenHeight(getContext());
	}
	
	public boolean onActivityResult(int requestCode, int resultCode,
            Intent intent) {
		return false;
	}
	
	protected void onBackKeyDown() {
		if (mListener != null) {
			mListener.onBackKeyDown(this);
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& keyCode == KeyEvent.KEYCODE_BACK) {
			TLog.v("tframe back key down");
			onBackKeyDown();
			return true;
		}
		return false;
	}

}
